package org.nowcoder.huawei.c100;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;

/**
 * 36:执行任务赚获取最多积分
 */
public class HC036 {
    public static void main(String[] args) {
        int n = 4;
        int t = 3;
        int[][] var = {{1, 2}, {1, 3}, {1, 4}, {3, 5}}; //最晚截止时间，对应积分

        List<Task> tasks = new ArrayList<>();

        for (int[] v : var) {
            tasks.add(new Task(v));
        }
        Collections.sort(tasks);
        solve(tasks,n,t);
    }

    static class Task implements Comparable<Task> {
        private int deadline;
        private int score;

        public Task(int[] task) {
            this.deadline = task[0];
            this.score = task[1];
        }

        @Override
        public int compareTo(Task o) {
            if (this.deadline == o.deadline) {
                return o.score - this.score;
            } else {
                return o.deadline - this.deadline;
            }
        }

        @Override
        public String toString() {
            return "Task{" +
                    "deadline=" + deadline +
                    ", score=" + score +
                    '}';
        }
    }

    public static void solve(List<Task> tasks, int n, int t) {
        PriorityQueue<Integer> que = new PriorityQueue<>(Collections.reverseOrder());
        int score = 0, count = 0;
        for (int i = 0; i < n; i++) {
            Task task = tasks.get(i);
            que.add(task.score);
            //将截止时间相同的任务放到任务队列中
            for (int j = i + 1; j < n; j++) {
                if (task.deadline == tasks.get(j).deadline) {
                    que.add(tasks.get(j).score);
                    i = j;
                } else {
                    break;
                }
            }
            //从队列中取出分数最大的
            score += que.poll();
            if (count > t) {
                break;
            }
        }
        System.out.println(score);
    }
}
